<?php
namespace app\admin\model;

use think\Model;

class Cate extends Model{

    private $cateModel;
    public function __construct(){
        parent::__construct();
        $this -> cateModel = db('cate');
    }
    /*  分页获取品牌列表
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function getCateDataListByWhere($where , $order = false , $num = 10 , &$page , $type =1 ){
        $cateData = $this -> cateModel -> where($where) -> order($order) -> paginate($num.false,['query'=>request()->param() ]);
        $page = $cateData -> render();
        $data = iterator_to_array($cateData);
        if(empty($data)){   return [];  }
        switch ($type){
            case 1:
                $dataList = $this -> formatCateDataForIndexList($data);
                break;
        }
        return $dataList;
    }
    /*  格式化品牌列表
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formatCateDataForIndexList($data){
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['name'] = $value['name'];
            $list[$key]['order'] = $value['order'];
            $list[$key]['status'] = $value['status'];
            //子分类
            $list[$key]['child'] = $this -> getCateList(['pid'=>$value['id']],'order desc,id asc',1);
        }
        return $list;
    }
    /*  获取行业分类
     *  @param Array [where]
     *  @return Array [dataList]
     * **/
    public function getCateList($where = false , $order  = false ,$type = 1){
        $cateData = $this -> cateModel -> where($where) -> order($order) -> select();
        if(empty($cateData)){   return [];  }
        switch ($type) {
            case 1:
                $dataList = $this -> formatCateData($cateData);
                break;
            case 2:
                $dataList = $this -> formatCateDataForNameId($cateData);
                break;
            default:
                # code...
                break;
        }
        return $dataList;

    }
    /*  格式化行业分类数据
     *  @param Array    [data]
     *  @return Array   [data]
     * **/
    public  function formatCateData($data){
        foreach ($data as $key => $value) {
            $dataList[$key]['id'] = $value['id'];
            $dataList[$key]['name'] = $value['name'];
            $dataList[$key]['img'] = $value['img'];
            $dataList[$key]['order'] = $value['order'];
            $dataList[$key]['status'] = $value['status'];
            $dataList[$key]['pid'] = $value['pid'];
        }
        return $dataList;
    }
    /*  格式化行业分类数据 [仅有名字和id]
     *  @param Array    [data]
     *  @return Array   [data]
     * **/
    public  function formatCateDataForNameId($data){
        foreach ($data as $key => $value) {
            $dataList[$key]['id'] = $value['id'];
            $dataList[$key]['name'] = $value['name'];
        }
        return $dataList;
    }
    /*  新增行业分类
     *  @param Array    [data]
     *  @return bool   result
     * **/
    public  function add($data){
        $result = $this -> cateModel -> insertGetId($data);
        return $result;
    }
    /*  新增行业分类
     *  @param Array    [where]
     *  @param Array    [save]
     *  @return bool   result
     * **/
    public function modify($where,$save){
        $result = $this -> cateModel -> where($where) -> update($save);
        return $result;
    }

    /*  查询行业分类
     *  @param Array    [where]
     *  @return Array   [info]
     * **/
    public function getCateData($where){
        $info = $this -> cateModel -> where($where) -> find();
        return $info;
    }


}